/*set working directory*/
/*ssc install blindschemes*/
/*findit renvars*/

/*Figure 5.1: Contrasting Population Density Trends in Haiti and Uganda, 1993-2013*/

use "Data/TSMOD STATA 2.dta", replace
fsum, label 
gen usahq=.
replace usahq=1 if hq1==12
replace usahq=1 if hq1==14
replace usahq=1 if hq1==15
replace usahq=1 if hq1==16
replace usahq=0 if usahq==.

drop if usahq==0

collapse (sum) algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav, by(year)

renvars algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav, pref(country_) 

reshape long country_, i(year) j(country) string
rename country_ usa_ingos
egen usaingosbar = mean(usa_ingos), by(year)
label variable usa_ingos "Total Number"
drop if year<1992	  
graph twoway (line usa_ingos year if country=="haiti", lwidth(medthick) lpattern(solid)) (line usa_ingos year if country=="uganda", lwidth(medthick) lpattern(dash)) (line usaingosbar year, lwidth(medthick) lpattern(dot)), xlabel(1990(10)2015) xtitle("") scale(1.5) scheme(plotplain) legend(off)
*Manually label countries using Greyscale6, Greyscale8, and Greyscale10
graph export "Figures/Figure5-1.pdf", as(pdf) replace

/*Figure 5.2: Density of American INGOs by Country, 2020*/

*This map was hand created using the historical country boundaries available at: https://historicalmapchart.net/index.html. The data are from our original coding of American INGOs' programmatic locations, contained in "Data/American INGOs Geo Data with Attributes 2020.dta"


/*Figure 5.3: American INGOs' Global Spread, 1953-2013*/
clear
use "Data/TSMOD STATA 2.dta"
fsum, label 
gen usahq=.
replace usahq=1 if hq1==12
replace usahq=1 if hq1==14
replace usahq=1 if hq1==15
replace usahq=1 if hq1==16
replace usahq=0 if usahq==.
drop if usahq==0

collapse (sum) algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav, by(year)

renvars algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav, pref(country_) 

reshape long country_, i(year) j(country) string
rename country_ usa_ingos
generate number=0
replace number=1 if usa_ingos>0
collapse (sum) number, by(country year)
collapse (mean) number, by(year)
label variable number "Proportion of Countries"
label variable year "Year"
line number year, scale(1.5) lwidth(medthick) xtitle("") scheme(plotplain) xlabel(1950(10)2020)
graph export "Figures/Figure5-3.pdf", as(pdf) replace

/*Figure 5.4: Correlates of American INGOs' Geographic Choices in 2020*/

clear
use "Data/American INGOs Geo Data with Attributes 2020.dta"

encode name, gen(orgid)
sort orgid
gen scode2=1
gen scode20=1
gen scode31=1
gen scode40=1
gen scode41=1 
gen scode42=1 
gen scode51=1 
gen scode52=1 
gen scode53=1 
gen scode54=1 
gen scode55=1 
gen scode56=1 
gen scode57=1
gen scode58=1  
gen scode60=1 
gen scode70=1 
gen scode80=1 
gen scode90=1 
gen scode91=1 
gen scode92=1 
gen scode93=1 
gen scode94=1 
gen scode95=1 
gen scode100=1 
gen scode101=1 
gen scode110=1 
gen scode115=1 
gen scode130=1 
gen scode135=1 
gen scode140=1 
gen scode145=1 
gen scode150=1 
gen scode155=1 
gen scode160=1 
gen scode165=1 
gen scode200=1 
gen scode205=1
gen scode210=1
gen scode211=1
gen scode212=1
gen scode220=1
gen scode221=1
gen scode223=1
gen scode225=1
gen scode230=1
gen scode232=1
gen scode235=1
gen scode255=1
gen scode260=1
gen scode265=1
gen scode290=1
gen scode305=1
gen scode310=1
gen scode315=1
gen scode316=1
gen scode317=1
gen scode325=1
gen scode331=1
gen scode338=1
gen scode339=1
gen scode341=1
gen scode345=1
gen scode348=1
gen scode343=1
gen scode344=1
gen scode346=1
gen scode347=1
gen scode349=1
gen scode350=1
gen scode352=1
gen scode355=1
gen scode359=1
gen scode360=1
gen scode365=1
gen scode366=1
gen scode367=1
gen scode368=1
gen scode369=1
gen scode370=1
gen scode371=1
gen scode372=1
gen scode373=1
gen scode375=1
gen scode380=1
gen scode385=1
gen scode390=1
gen scode395=1
gen scode402=1
gen scode403=1
gen scode404=1
gen scode411=1
gen scode420=1
gen scode432=1
gen scode433=1
gen scode434=1
gen scode435=1
gen scode436=1
gen scode437=1
gen scode438=1
gen scode439=1
gen scode450=1
gen scode451=1
gen scode452=1
gen scode461=1
gen scode471=1
gen scode475=1
gen scode481=1
gen scode482=1
gen scode483=1
gen scode484=1
gen scode490=1
gen scode500=1
gen scode501=1
gen scode510=1
gen scode511=1
gen scode516=1
gen scode517=1
gen scode520=1
gen scode522=1
gen scode530=1
gen scode531=1
gen scode540=1
gen scode541=1
gen scode551=1
gen scode552=1
gen scode553=1
gen scode560=1
gen scode565=1
gen scode570=1
gen scode571=1
gen scode572=1
gen scode580=1
gen scode581=1
gen scode590=1
gen scode591=1
gen scode600=1
gen scode615=1
gen scode616=1
gen scode620=1
gen scode625=1
gen scode630=1
gen scode640=1
gen scode645=1
gen scode651=1
gen scode652=1
gen scode660=1
gen scode663=1
gen scode666=1
gen scode667=1
gen scode670=1
gen scode678=1
gen scode679=1
gen scode680=1
gen scode690=1
gen scode692=1
gen scode694=1
gen scode696=1
gen scode698=1
gen scode700=1
gen scode701=1
gen scode702=1
gen scode703=1
gen scode704=1
gen scode705=1
gen scode710=1
gen scode712=1
gen scode713=1
gen scode730=1
gen scode731=1
gen scode732=1
gen scode740=1
gen scode750=1
gen scode760=1
gen scode770=1
gen scode771=1
gen scode775=1
gen scode780=1
gen scode781=1
gen scode790=1
gen scode800=1
gen scode811=1
gen scode812=1
gen scode816=1
gen scode820=1
gen scode830=1
gen scode835=1
gen scode840=1
gen scode850=1
gen scode860=1
gen scode900=1
gen scode910=1
gen scode920=1
gen scode935=1
gen scode940=1
gen scode946=1
gen scode947=1
gen scode950=1
gen scode955=1
gen scode970=1
gen scode983=1
gen scode986=1
gen scode987=1
gen scode990=1

reshape long scode, i(orgid) j(countrycode)
gen membership=.
foreach x of varlist country1-country172 {
      replace membership=1 if `x'==countrycode
        }
replace membership=0 if membership==.
drop country1-country172
drop scode
rename countrycode scode
replace scode=255 if scode==260
replace scode=255 if scode==265
replace scode=316 if scode==315
replace scode=490 if scode==484
replace scode=666 if scode==667
replace scode=679 if scode==678
replace scode=679 if scode==680
replace scode=710 if scode==713
replace scode=732 if scode==730
replace scode=345 if scode==348
replace scode=510 if scode==511

encode state, generate(DC2)
recode DC2 (min/9=0) (10=1) (11=1) (12/31=0) (32=1) (32/59=0) (60=1) (61/max=0)
label drop DC2
label variable DC2 "DC Headquarters"

tempfile USA_INGOs_by_Country_2020
save `USA_INGOs_by_Country_2020'

/*generate density variables from the TSMOD*/
use "Data/TSMOD STATA 2.dta", clear
fsum, label 
keep if year==2013
encode orgname, gen(orgid)

foreach x of var algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav { 	
 rename `x' mem`x' 
} 

egen orgyear = concat(orgid year)
sort orgid year
quietly by orgid year:  gen dup = cond(_N==1,0,_n)
tabulate dup
drop if dup>1

drop seriesid yearbookid hqtxt members_1 members_2 members_3 members_4 members_5 finance_self finance_founds finance_corps finance_govigo finance_othr ident_1 ident_2 goal_txt cons1 cons2 cons3 cons4 cons5 cons_txt hq2 igo1 igo2 igo3 igo4 igo5 igo6 igo_txt othercountry entryyear yearbookcode fillyear count foundyrflag norso after89 oecd1 dup 

reshape long mem, i(orgyear) j(country, string)
gen sname=country
gen scode=.
run "Scode Transformation.do"
drop if scode==.
replace scode=255 if scode==260
replace scode=255 if scode==265
replace scode=316 if scode==315
replace scode=679 if scode==678
replace scode=679 if scode==680
replace scode=732 if scode==730
replace scode=510 if scode==511

gen usahq=. 
replace usahq=1 if hq1==12
replace usahq=1 if hq1==14
replace usahq=1 if hq1==15
replace usahq=1 if hq1==16
replace usahq=0 if usahq==.
keep if usahq==1

collapse (sum) mem (max) scode, by (country)
rename mem usa_total_ingos
sort scode
tempfile USA_INGOs_Country_Sector_2013
save `USA_INGOs_Country_Sector_2013'

use `USA_INGOs_by_Country_2020', clear
sort scode
merge m:1 scode using `USA_INGOs_Country_Sector_2013'
replace usa_total_ingos=0 if _merge==1
drop if _merge==2
drop _merge
tempfile formerging
save `formerging'

use "Data/V-Dem-CY-Full+Others-v12.dta", clear
keep if year==2019
keep country_name year COWcode v2csreprss e_wbgi_cce e_wb_pop e_gdppc 
rename COWcode scode
sort scode
drop if scode==.
merge 1:m scode using `formerging'
drop _merge
tempfile formerging
save `formerging', replace

clear
import excel "Data/us_foreignaid_greenbook.xlsx", sheet("Economic and Military Aid") firstrow
keep if AssistanceCategory=="Economic"
rename FiscalYear year
keep if year==2019
collapse (sum) ObligationsConstantDollars, by(Country)
rename Country sname
generate scode=.
run "Scode Transformation.do"
drop if scode==.
rename sname country
rename ObligationsConstantDollars us_aid
sort scode 
merge 1:m scode using `formerging'
replace us_aid=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging', replace

use "Data/atop5_1dy.dta", clear
keep if mem1==2
keep if year==2018
replace year=year+1
keep mem2 year atopally
rename mem2 scode
sort scode
merge 1:m scode using `formerging'
replace atopally=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging', replace

clear
import excel "Data/Data Set Tracking Media Attention in the US to Countries (NY Times).xlsx", firstrow 
keep Scode Country year2019
rename Scode scode
rename Country country
rename year2019 media
duplicates tag scode, gen(isdup) 
drop if isdup>0
sort scode
merge 1:m scode using `formerging'

renvars usa_total_ingos, postfix(_2013)
gen usa_total_ingos_2013_sq=usa_total_ingos_2013^2

/*Analysis*/

drop if scode==2
gen lnaid=ln(us_aid+1) 
gen lnrevenue=ln(revenue+1)
gen lnpop=ln(e_wb_pop+1)
gen lngdp=log(e_gdppc+1)
gen lnmedia=ln(media+1)

egen stdusingos=std(usa_total_ingos_2013)
egen stdusingos_sq=std(usa_total_ingos_2013_sq)

label variable lnrevenue "Revenue"
label variable geoscopenumeric "Geographic Scope"
label variable faithbased "Faith-based"
label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lngdp "GDP per capita"
label variable lnpop "Population"
label variable e_wbgi_cce "Control of Corruption"
label variable v2csreprss "CSO Repression"
label variable atopally "U.S. Ally"
label variable stdusingos "Density"
label variable stdusingos_sq "Density Squared"

*analysis

reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Figure5-4.pdf", as(pdf) replace

/*Figure 5.5: Correlates of American INGOs' Geographic Choices 1993-2013*/

use "Data/TSMOD STATA 2.dta", clear
fsum, label 

encode seriesid, gen(orgid)
foreach x of var algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav { 	
 rename `x' mem`x' 
} 

egen orgyear = concat(orgid year)

sort orgid year
quietly by orgid year:  gen dup = cond(_N==1,0,_n)
tabulate dup
drop if dup>1

drop seriesid yearbookid hqtxt members_1 members_2 members_3 members_4 members_5 finance_self finance_founds finance_corps finance_govigo finance_othr ident_1 ident_2 goal_txt cons1 cons2 cons3 cons4 cons5 cons_txt hq2 igo1 igo2 igo3 igo4 igo5 igo6 igo_txt othercountry entryyear yearbookcode fillyear count foundyrflag norso after89 oecd1 dup 

reshape long mem, i(orgyear) j(country, string)
gen sname=country
gen scode=.
run "Scode Transformation.do"
drop if scode==.
replace scode=255 if scode==260
replace scode=255 if scode==265
replace scode=316 if scode==315
replace scode=679 if scode==678
replace scode=679 if scode==680
replace scode=732 if scode==730
replace scode=510 if scode==511
sort orgid country year
by orgid country: gen mem_lag2=mem[_n-1]
by orgid country: gen mem_lag4=mem[_n-2]
tempfile Org_Country_Year
save `Org_Country_Year'

gen usahq=. 
replace usahq=1 if hq1==12
replace usahq=1 if hq1==14
replace usahq=1 if hq1==15
replace usahq=1 if hq1==16
replace usahq=0 if usahq==.
keep if usahq==1
tempfile USA_Org_Country_Year
save `USA_Org_Country_Year'

use `Org_Country_Year', clear
collapse (sum) mem (max) scode, by (country year)
rename mem total_ingos
sort year scode
tempfile INGOs_Country_Year_Sector
save `INGOs_Country_Year_Sector'

use `USA_Org_Country_Year', clear
collapse (sum) mem (max) scode, by (country year)
rename mem usa_total_ingos
sort year scode
tempfile USA_INGOs_Country_Year_Sector
save `USA_INGOs_Country_Year_Sector'

use `USA_Org_Country_Year', clear
sort year scode
merge m:1 year scode using `INGOs_Country_Year_Sector'
drop _merge
sort year scode
merge m:1 year scode using `USA_INGOs_Country_Year_Sector'
drop _merge
sort scode year
tempfile formerging
save `formerging'

clear
import excel "Data/us_foreignaid_greenbook.xlsx", sheet("Economic and Military Aid") firstrow
keep if AssistanceCategory=="Economic"
rename FiscalYear year
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
collapse (sum) ObligationsConstantDollars, by(Country year)
rename Country sname
generate scode=.
run "Scode Transformation.do"
drop if scode==.
rename sname country
drop if country=="Serbia and Montenegro, Former" & year==2007 /*to remove a duplicate observation*/
rename ObligationsConstantDollars us_aid
sort scode year
merge 1:m scode year using `formerging'
replace us_aid=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging'

use "Data/V-Dem-CY-Full+Others-v12.dta", clear
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
keep country_name year COWcode v2csreprss e_wbgi_cce e_wb_pop e_gdppc
rename COWcode scode
drop if scode==.
sort scode year
merge 1:m scode year using `formerging'
drop _merge
tempfile formerging
save `formerging'

clear
import excel "Data/Data Set Tracking Media Attention in the US to Countries (NY Times).xlsx", firstrow 
rename Scode scode
rename Country country
duplicates tag scode, gen(isdup) 
drop if isdup>0
drop P Q R S T U V W X Y Z isdup
reshape long year, i(scode) j(variable)
rename year media
rename variable year
drop if year==1991 | year==2019
sort scode year
merge 1:m scode year using `formerging'
drop _merge
tempfile formerging
save `formerging'

use "Data/atop5_1dy.dta", clear
keep if mem1==2
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
keep mem2 year atopally
rename mem2 scode
sort scode year
merge 1:m scode year using `formerging'
replace atopally=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging'

clear
import delimited "Data/Dyadic_COW_4.0.csv"
keep if importer1=="United States of America"
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
rename ccode2 scode
sort scode year
merge 1:m scode year using `formerging'
replace flow1=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging'

use "Data/NGOrepression.dta", clear 
sort ccode year
quietly by ccode year:  gen dup = cond(_N==1,0,_n)
drop if dup==2
drop dup
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
rename ccode scode
sort scode year
merge 1:m scode year using `formerging'
drop if _merge==1
drop _merge

gen total_ingos_sq=total_ingos^2
gen usa_total_ingos_sq=usa_total_ingos^2
tempfile TSMO_Data_for_Lagged_Merges
save `TSMO_Data_for_Lagged_Merges'

collapse total_ingos total_ingos_sq usa_total_ingos usa_total_ingos_sq, by(scode year)
tsset scode year

gen total_ingos_L2=L2.total_ingos
gen total_ingos_L4=L4.total_ingos
gen total_ingos_L6=L6.total_ingos
gen total_ingos_sq_L2=L2.total_ingos_sq
gen total_ingos_sq_L4=L4.total_ingos_sq
gen total_ingos_sq_L6=L6.total_ingos_sq
gen usa_total_ingos_L2=L2.usa_total_ingos
gen usa_total_ingos_L4=L4.usa_total_ingos
gen usa_total_ingos_L6=L6.usa_total_ingos
gen usa_total_ingos_sq_L2=L2.usa_total_ingos_sq
gen usa_total_ingos_sq_L4=L4.usa_total_ingos_sq
gen usa_total_ingos_sq_L6=L6.usa_total_ingos_sq
tempfile country_level_ingo_merges
save `country_level_ingo_merges'

use `TSMO_Data_for_Lagged_Merges', clear

sort scode year
merge m:1 scode year using `country_level_ingo_merges'
drop _merge

gen lnaid=ln(us_aid+1) 
gen lnpop=ln(e_wb_pop+1)
gen lngdp=log(e_gdppc+1)
gen lnmedia=ln(media+1)

* set up 
drop if scode==2

egen stdusingos_L2=std(usa_total_ingos_L2)
egen stdusingos_sq_L2=std(usa_total_ingos_sq_L2)

egen stdusingos_L4=std(usa_total_ingos_L4)
egen stdusingos_sq_L4=std(usa_total_ingos_sq_L4)

egen stdusingos_L6=std(usa_total_ingos_L6)
egen stdusingos_sq_L6=std(usa_total_ingos_sq_L6)

egen stdmem_lag2=std(mem_lag2)
egen stdmem_lag4=std(mem_lag4)

label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lngdp "GDP per capita"
label variable lnpop "Population"
label variable v2csreprss "CSO Repression"
label variable atopally "U.S. Ally"
label variable stdusingos_L4 "Density"
label variable stdusingos_sq_L4 "Density Squared"
label variable stdmem_lag2 "Lagged DV"
label variable stdusingos_L2 "Density"
label variable stdusingos_sq_L2 "Density Squared"
label variable stdusingos_L6 "Density"
label variable stdusingos_sq_L6 "Density Squared"
label variable year "Year"
label variable age "Age"

egen panelvar = group(scode orgid)
sort panelvar year
quietly by panelvar year:  gen dup = cond(_N==1,0,_n)
drop if dup>0
xtset panelvar year
keep if year>1992

/*Linear Probability Model*/
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Figure5-5.pdf", as(pdf) replace
